<?php
/**
 * Created by PhpStorm.
 * User: zhou
 * Date: 2017/1/3
 * Time: 15:54
 */
namespace Application\Utils;
class Page
{
    public function createPage($pageIndex,$pageNum){
        $href = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $href = preg_replace('/&*'.'page'.'=\d*/', '', $href);
        $href = str_replace('%', '#', $href);
        if (! strstr($href, '?')) {
            $href .= '?';
        }
        if (substr($href, - 1) != '?') {
            $href .= '&';
        }
        $html = '';
        $i = 1;
        if(($pageIndex/10) >= 1){
            $i = $pageIndex-5;
            if(($i+10) >$pageNum){
                $length = $pageNum;
            }else{
                $length = $i+10;
            }
        }else {
            if($pageNum>=10){
                $length = 10;
            }else {
                $length = $pageNum;
            }
        }
        for($i;$i <=$length;$i++) {
            $pager = "page=";
            if ($pageIndex == $i) {
                $html .= "<li class='active'><span>{$i}<span class='sr-only'></span></span></li>";
            } else {
                $html .= "<li><a href='{$href}{$pager}{$i}'>{$i}</a></li>";
            }
        }
        $html = "<ul class='pagination'><li><a href='{$href}{$pager}1'>首页</a></li>" . $html;
        $html .= "<li><a href='{$href}{$pager}{$pageNum}'>尾页</a></li></ul>";
        return $html;
    }
}